package org.jeecg.modules.demo.document.utils;



import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
//import org.bouncycastle.util.encoders.Base64;
import org.apache.commons.codec.binary.Base64;

@Slf4j
public class PDFFileUtils {
    /**
     * 方法名:          PDFToBase64
     * 方法功能描述:     将pdf文件转换为Base64编码
     * @param:        String sourceFile:源文件路径
     * @return:       String  base64字符串
     * @Author:       zhouhu
     * @Create Date:   2019年07月03日
     */
    public static byte[] PDFToBase64(String sourceFile) {
        FileInputStream fin =null;
        BufferedInputStream bin =null;
        ByteArrayOutputStream baos = null;
        BufferedOutputStream bout =null;
        File file = new File(sourceFile);
        try {
            fin = new FileInputStream(file);
            bin = new BufferedInputStream(fin);
            baos = new ByteArrayOutputStream();
            bout = new BufferedOutputStream(baos);
            byte[] buffer = new byte[1024];
            int len = bin.read(buffer);
            while(len != -1){
                bout.write(buffer, 0, len);
                len = bin.read(buffer);
            }
            //刷新此输出流并强制写出所有缓冲的输出字节
            bout.flush();
            byte[] bytes = baos.toByteArray();
            return bytes;
//            return encoder.encodeBuffer(bytes).trim();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                fin.close();
                bin.close();
                bout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * @Description:Base64转换成pdf
     * @param:
     * @return:
     * @author: TateBrown
     * @Date: 2018/7/23
     */
    public static void BasetoPdffile(String filepath,byte[] bytes,String nfilepath){
        String pdfurl = "/opt/upFiles/";
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        File file1=null;

        try{
//            getFile(bytes,pdfurl,filepath);
//            FileUtils.writeByteArrayToFile(new File(pdfurl+filepath),bytes);
            ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(bytes);
            System.setProperty("sun.jnu.encoding","UTF-8");
            String file_name = pdfurl+filepath;
            try {
                file_name = new String(file_name.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            bis=new BufferedInputStream(byteArrayInputStream);
            file1=new File(file_name);
//            System.out.println(System.getProperty("sun.jnu.encoding"));
            log.info("file_name"+file_name);
//            System.out.println();
            File path=file1.getParentFile();
            if(!path.exists()){
                path.mkdirs();
            }
            fos=new FileOutputStream(file1);
            bos=new BufferedOutputStream(fos);

            byte[] buffer=new byte[1024];
            int length=bis.read(buffer);
            while(length!=-1){
                bos.write(buffer,0,length);
                length=bis.read(buffer);
            }
            bos.flush();
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            try{
                bis.close();
                bos.close();
                fos.close();
            }catch (IOException e){
                e.printStackTrace();
            }

        }


    }


    public static void main(String[] args) {

//        PDFToBase64("/opt/upFiles/你好.pdf");
        BasetoPdffile("/opt/upFiles/你好1.pdf",PDFToBase64("/opt/upFiles/你好.pdf"),"");
    }
}
